package com.shq.ex1.game;

import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shq.ex1.R;

public class PlayActivity extends Activity {

	private int count = 0;//打到的地鼠的数量
	private int time = 6000;//总时间
	private ImageView mouse = null;//地鼠
	private Thread thread = null;
	private boolean normal = true;//是否正常结束游戏
	private Handler handler; // 声明一个Handler对象
	private TextView timeText = null;//显示时间
	private int[][] position = new int[][]{{87,120},{263,120},{431,120},{587,120},{15,232},{160,232},{309,232},{470,232},{625,232}};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mouse = (ImageView) findViewById(R.id.mouse);
		timeText = (TextView) findViewById(R.id.time);

		mouse.setOnTouchListener(new OnTouchListener() {//为地鼠图片添加监听

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				v.setVisibility(View.INVISIBLE); // 设置地鼠不显示
				count++;
				Toast.makeText(PlayActivity.this, "打到[ " + count + " ]只地鼠！",
						Toast.LENGTH_SHORT).show(); // 显示消息提示框
				return false;
			}
		});
		handler = new Handler() {
			public void handleMessage(Message msg) {
				int index = 0;
				if (time >0 && normal == true) {
					if (msg.what == 0x101) {
						timeText.setText("剩余时间为：" + time/100 );
						index = msg.arg1; // 获取位置索引值
						mouse.setX(position[index][0]);
						mouse.setY(position[index][1]);
						mouse.setVisibility(View.VISIBLE); // 设置地鼠显示
					}
					super.handleMessage(msg);

				}else if(normal == true)
				{
					gameOver();
				}
			}
		};
		thread = new MyThread();
		thread.start(); // 开启线程
	}
	public void gameOver(){//游戏结束
		timeText.setText("时间到！");
		handler.removeCallbacks(thread);
		thread = null;
		new AlertDialog.Builder(PlayActivity.this).setTitle("游戏结束").setMessage("您一共打到" + count + "只地鼠")
				.setNegativeButton("再来一局", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						thread = new MyThread();
						time = 6000;
						count = 0;
						thread.start(); // 开启线程
					}
				})
				.setNeutralButton("退出", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						PlayActivity.this.finish();
					}
				}).create().show();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			normal = false;
			PlayActivity.this.finish();
		}
		return true;
	}

	public class MyThread extends Thread{
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			int index = 0 ; // 创建一个记录地鼠位置的索引值
			while (!Thread.currentThread().isInterrupted()) {
				index = new Random().nextInt(position.length); // 产生一个随机数
				Message m = handler.obtainMessage(); // 获取一个Message
				m.what = 0x101; // 设置消息标识
				m.arg1 = index; // 保存地鼠标位置的索引值
				handler.sendMessage(m); // 发送消息
				if(time > 0 && normal == true){
					try {
						Thread.sleep(new Random().nextInt(1000) + 1000); // 休眠一段时间
						time = time - 100;
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}else{
					thread.interrupt();
				}
			}
		}
	}
}
